/*
startUp.sqf
Author: Craig Vander Galien feat. M&M
Email: craigvandergalien@gmail.com
ModDB: http://www.moddb.com/mods/bobtoms-arma2-missions
Armaholic: http://www.armaholic.com/page.php?id=15778
BIS forums: http://forums.bistudio.com/showthread.php?128781-TvT-FFA-CO-40-Dynamic-Zombie-Sandbox

Any questions? Contact me at one of the above places.

Thanks,
Craig
*/

private ["_options","_value","_side","_group","_logic","_type","_weap_type","_cur_type","_display_name","_no_pack","_optics","_weapon","_cfgweapons","_item_type","_marker","_pos","_trig","_building","_xaxis","_yaxis","_working","_allaxiss","_newPos","_townnumber","_town","_townpos","_rad","_cnps","_towns","_playerRespawn","_check2","_check1","_thing","_things","_checkVar","_dist","_dir","_armPos","_centPos"];

//Starts the loading screen that was defined in the description.ext
StartLoadingScreen["Loading Mission Components","RscLoadScreenCustom"];


/*
This part declares all the variables for the mission. It gets the values from the chosen parameters
*/

private ["_options","_value","_num"];
CLY_friendlyfire=paramsArray select 0;
CLY_terraingrid=paramsArray select 1;
CVG_debug= (paramsArray select 2);
CVG_timeToSkipTo = (paramsArray select 3);
CVG_CityDestruction= (paramsArray select 4);
CVG_bandages= (paramsArray select 5);
CVG_Fog = (paramsArray select 6);
CVG_Viewdist = (paramsArray select 7);
CVG_Weather = (paramsArray select 8);
gameType = (paramsArray select 9);
CVG_Playerstart = (paramsArray select 10); 
CVG_playerWeapons = (paramsArray select 11); 
CVG_playerItems= (paramsArray select 12); 
CVG_Aminals= (paramsArray select 13); 
CVG_horde= (paramsArray select 14);
CVG_maxaggroradius = (paramsArray select 15);
CVG_Zdensity = (paramsArray select 16);
CVG_minSpawnDist = (paramsArray select 17);
CVG_weapontype = (paramsArray select 18);
CVG_Zombietowns= (paramsArray select 19);
CVG_taskType = (paramsArray select 20);
CVG_taskOption = (paramsArray select 21);
vehspawntype = (paramsArray select 22);
chanceNumber = (paramsArray select 23);
CVG_Fuel = (paramsArray select 24);
CVG_fastTime = (paramsArray select 25);
CVG_Weaponcount = (paramsArray select 26);
CVG_VehicleStatus = (paramsArray select 27);
CVG_Respawn = (paramsArray select 28);
CVG_Mochilla = (paramsArray select 29);
CVG_Caches = (paramsArray select 30);
CVG_SideMissions= (paramsArray select 31);
CVG_SideMarkers= (paramsArray select 32);
CVG_Survivors= (paramsArray select 33);
CVG_Logistics= (paramsArray select 34);
CVG_NVG= (paramsArray select 35);
CVG_E= (paramsArray select 36);
CVG_Helis= (paramsArray select 37);
WF_Modif = (paramsArray select 38);

// This part, if random was chosen, executes the randomness

if (CVG_timeToSkipTo == 25) then {
    CVG_timeToSkipTo = floor (random 24);
};
if (CVG_playerWeapons == 3) then {
    _options = [1,2];
    _value = floor (random (count _options));
    CVG_playerWeapons= _value;
}; 
if (CVG_playerItems == 3) then {
    _options = [1,2];
    _value = floor (random (count _options));
    CVG_playerItems= _value;
};
if (CVG_maxaggroradius == 9999) then {
    CVG_maxaggroriadius=floor (random 1000);
};
if (CVG_Zdensity == 99) then {
    CVG_Zdensity = floor (random 98);
};
if (CVG_minSpawnDist == 999) then {
    CVG_minSpawnDist = floor (random 100);
};
if (CVG_Weapontype == 7) then {
    CVG_weapontype= floor (random 6);
};
if (chanceNumber == 99) then {
    chanceNumber = floor (random 30);
};
if (CVG_Fuel == 3) then {
    CVG_Fuel = floor (random 1);
};
if (CVG_Weaponcount == 99) then {
    _options = [20,50,75,101];
    _value = floor (random (count _options));
    CVG_Weaponcount= _value;
};
if (CVG_VehicleStatus == 4) then {
    _options = [1,2,3,4];
    _value = floor (random (count _options));
    CVG_VehicleStatus= _value;
};
//delares that  variable assigning is complete, needed in init.sqf
paramsdone = 1;

// if there is no functions module, it creates one

if (isNil "BIS_functions_mainscope") then
{
    _side = createCenter sideLogic;
    _group = createGroup _side;
    _logic = _group createUnit ["FunctionsManager", [0,0,0], [], 0, "NONE"];
};
waitUntil {!isnil "bis_fnc_init"};

//////////////////////////////////
//Section 1 Done!
progressLoadingScreen 0.2;
//////////////////////////////////

_rad=20000;
_cnps = getArray (configFile >> "CfgWorlds" >> worldName >> "centerPosition");
townlist= nearestLocations [_cnps, ["CityCenter"], _rad];

//this part goes into the cfgWeapons and grabs all the useable weapons classnames and puts them into arrays based on type


CVG_Rifles = [];
CVG_scoped = [];
CVG_heavy = [];
CVG_launchers = [];
CVG_pistols = [];
CVG_Explosives = [];
CVG_Binoculars = [];
CVG_SmallItems = [];

{
    _cfgweapons = configFile >> "cfgWeapons";
    
    _type = 1;
    _item_type = _x;
    switch (_item_type) do {
        case 0: {_type = [1];};//CVG_Rifles
        case 1: {_type = [1];};//scoped CVG_Rifles
        case 2: {_type = [1,5];};//heavy
        case 3: {_type = [4];};//secondary weapon
        case 4: {_type = [2];};//pistol
        case 5: {_type = [0];};//put/throw
        case 6: {_type = [4096];};//BinocularSlot
        case 7: {_type = [131072];};//SmallItems
        default {_type = [1];};
    };
    
    for "_i" from 0 to (count _cfgweapons)-1 do {
        _weapon = _cfgweapons select _i;
        if (isClass _weapon) then {
            _weap_type = configName(_weapon);
            _cur_type = getNumber(_weapon >> "type");
            _display_name = getText(_weapon >> "displayName");
            _no_pack = getNumber(_weapon >> "ACE_nopack");
            _optics = getText(_weapon >> "ModelOptics");
            
            if (((((getNumber(_weapon >> "scope")==2)&&(getText(_weapon >> "model")!="")&&(_display_name!=""))||((_item_type==5)&&(getNumber(_weapon >> "scope")>0)))&&(_cur_type in _type)&&(_display_name!=""))
            &&
            ((_item_type in [3,4,5,6,7])||((_item_type==0)&&(_no_pack!=1)&&((_optics=="-")))||((_item_type==1)&&(_no_pack!=1)&&((_optics!="-")))||((_item_type==2)&&((_cur_type==5)||((_no_pack==1)&&(_cur_type in _type)))))) then {
                
                if (_item_type == 0) then {
                    CVG_Rifles set [(count CVG_Rifles),_weap_type];//CVG_Rifles
                };
                if (_item_type == 1) then {
                    CVG_Scoped set [(count CVG_Scoped),_weap_type];//CVG_Scoped
                };
                if (_item_type == 2) then {
                    CVG_Heavy set [(count CVG_Heavy),_weap_type];//CVG_Heavy
                };
                if (_item_type == 3) then {
                    CVG_Launchers set [(count CVG_Launchers),_weap_type];//CVG_Launchers
                };
                if (_item_type == 4) then {
                    CVG_Pistols set [(count CVG_Pistols),_weap_type];//CVG_Pistols
                };
				if (_item_type == 5) then {
                    CVG_Explosives set [(count CVG_Pistols),_weap_type];//CVG_Pistols
                };
				if (_item_type == 5) then {
                    CVG_Explosives set [(count CVG_Explosives),_weap_type];//CVG_Explosives
                };
				if (_item_type == 6) then {
                    CVG_Binoculars set [(count CVG_Binoculars),_weap_type];//CVG_Binoculars
                };
				if (_item_type == 7) then {
                    CVG_SmallItems set [(count CVG_SmallItems),_weap_type];//CVG_Binoculars
                };
                
            };
        };
    };
    
} forEach [0,1,2,3,4,5,6,7];

#define KINDOF_ARRAY(a,b) [##a,##b] call {_veh = _this select 0;_types = _this select 1;_res = false; {if (_veh isKindOf _x) exitwith { _res = true };} forEach _types;_res}

timeAtStart = time;
private ["_kindOf","_filter","_res","_veh","_types","_veh_type","_vehicle","_typeNumbers","_cfgvehicles"];
CVG_statics = [];
CVG_Cars = [];
CVG_trucks = [];
CVG_APCs = [];
CVG_tanks = [];
CVG_Helicopters = [];
CVG_Planes = [];
CVG_Ships = [];
{
    _veh_type = [];
    _typeNumbers = _x;
    _kindOf = ["tank"];
    _filter = [];
    switch (_typeNumbers) do {
        case 0: {_kindOf = ["staticWeapon"];};
        case 1: {_kindOf = ["car"];_filter = ["truck","Wheeled_APC"];};
        case 2: {_kindOf = ["truck"];};
        case 3: {_kindOf = ["Wheeled_APC","Tracked_APC"];};
        case 4: {_kindOf = ["tank"];_filter = ["Tracked_APC"];};
        case 5: {_kindOf = ["helicopter"];_filter = ["ParachuteBase"];};
        case 6: {_kindOf = ["plane"];_filter = ["ParachuteBase"];};
        case 7: {_kindOf = ["ship"];};
        default {_kindOf = ["tank"];_filter = [];};
    };
    _cfgvehicles = configFile >> "cfgVehicles";
    for "_i" from 0 to (count _cfgvehicles)-1 do {
        _vehicle = _cfgvehicles select _i;
        if (isClass _vehicle) then {
            _veh_type = configName(_vehicle);
            if ((getNumber(_vehicle >> "scope")==2)and(getText(_vehicle >> "picture")!="")and(KINDOF_ARRAY(_veh_type,_kindOf))and!(KINDOF_ARRAY(_veh_type,_filter))) then {
                
                if (_typeNumbers == 0) then {
                    CVG_statics set [(count CVG_statics),_veh_type];//CVG_statics
                };
                if (_typeNumbers == 1) then {
                    CVG_Cars set [(count CVG_Cars),_veh_type];//CVG_Cars
                };
                if (_typeNumbers == 2) then {
                    CVG_Trucks set [(count CVG_Trucks),_veh_type];//CVG_Trucks
                };
                if (_typeNumbers == 3) then {
                    CVG_APCs set [(count CVG_APCs),_veh_type];//CVG_APCs
                };
                if (_typeNumbers == 4) then {
                    CVG_Tanks set [(count CVG_Tanks),_veh_type];//CVG_tanks
                };
                if (_typeNumbers == 5) then {
                    CVG_Helicopters set [(count CVG_Helicopters),_veh_type];//CVG_Helis
                };
                if (_typeNumbers == 6) then {
                    CVG_Planes set [(count CVG_Planes),_veh_type];//CVG_planes
                };
                if (_typeNumbers == 7) then {
                    CVG_Ships set [(count CVG_Ships),_veh_type];//CVG_Ships
                };
                
            };
        };
    };
} forEach [1,2,3,5,7];
/*
player sideChat format ["Time taken = %1 seconds",(round (time - timeAtStart))];
player sideChat format ["Count of Cars is: %1",count CVG_Cars];
player sideChat format ["Count of trucks is: %1",count CVG_trucks];
player sideChat format ["Count of APCs is: %1",count CVG_APCs];
player sideChat format ["Count of Tanks is: %1",count CVG_Tanks];
player sideChat format ["Count of Helicopters is: %1",count CVG_Helicopters];
player sideChat format ["Count of Planes is: %1",count CVG_Planes];
player sideChat format ["Count of Ships is: %1",count CVG_Ships];
*/







gunsComplete = 1;

//Finds all the towns on the map




//Task script and ammo cache script
[] execVM "craigs_scripts\tasks.sqf"; 
[] execVM "craigs_scripts\AmmoCaches.sqf";

//
//
//
//
//
//
fn_getBuildingPositions = {
    /*
    Author: 
    r�be
    
    Description
    returns a list of all available building positions
    
    Parameter(s):
    _this: building (object)
    
    Example:
    _positions = _building call RUBE_getBuildingPositions; 
    
    Returns:
    array of positions (guaranteed to return at least one position; position building)
    */
    
    private ["_building","_positions","_i","_next"];
    
    _building = _this;
    _positions = [];
    
    // search more positions
    _i = 1;
    while {_i > 0} do
    {
        _next = _building buildingPos _i;
        if (((_next select 0) == 0) && ((_next select 1) == 0) && ((_next select 2) == 0)) then
        {
            _i = 0;
        } else {
            _positions set [(count _positions), _next];
            _i = _i + 1;
        };
    };
    
    // return positions
    _positions
};

arrayCompare =
{
    private ["_array1","_array2","_i","_return"];
    
    _array1 = _this select 0;
    _array2 = _this select 1;
    
    _return = true;
    if ( (count _array1) != (count _array2) ) then
    {
        _return = false;
    }
    else
    {
        _i = 0;
        while {_i < (count _array1) && _return} do
        {
            if ( (typeName (_array1 select _i)) != (typeName (_array2 select _i)) ) then
            {
                _return = false;
            }
            else
            {
                if (typeName (_array1 select _i) == "ARRAY") then
                {
                    if (!([_array1 select _i, _array2 select _i] call arrayCompare)) then
                    {
                        _return = false;
                    };
                }
                else
                {
                    if ((_array1 select _i) != (_array2 select _i)) then
                    {
                        _return = false;
                    };
                };
            };
            _i = _i + 1;
        };
    };
    
    _return
};




if (CVG_Logistics == 1) then {
    objectList = ["Land_Campfire_burning","Land_BarGate2","Land_Fire_barrel_burning","Land_fort_bagfence_corner","Land_HBarrier5","Land_BagFenceLong","Fort_EnvelopeBig_EP1","Fort_EnvelopeSmall_EP1","Fort_StoneWall_EP1","Land_Fort_Watchtower_EP1","Land_Misc_IronPipes_EP1","Land_Misc_Rubble_EP1","Land_stand_small_EP1","Fort_RazorWire","Land_HBarrier_large","Land_HBarrier5","Fort_Barricade","Land_fort_artillery_nest","Land_fort_rampart","Land_fortified_nest_small","Land_fort_bagfence_round","Land_Ind_BoardsPack2"];
};

    //Set up cars:
    switch (vehSpawnType) do
    {
        case 0:
        {
            cars = ["Ikarus","SkodaBlue","SkodaGreen","SkodaRed","Skoda","VWGolf","TT650_Civ","MMT_Civ","hilux1_civil_2_covered","hilux1_civil_1_open","hilux1_civil_3_open","car_hatchback","datsun1_civil_1_open","datsun1_civil_2_covered","V3S_Civ","car_sedan","Tractor","UralCivil","UralCivil2","Lada_base","LadaLM","Lada2","Lada1","UAZ_CDF","Ural_CDF","hilux1_civil_3_open_EP1","Ikarus_TK_CIV_EP1","Lada1_TK_CIV_EP1","Lada2_TK_CIV_EP1","Old_bike_TK_CIV_EP1","Old_moto_TK_Civ_EP1","S1203_TK_CIV_EP1","SUV_TK_CIV_EP1","TT650_TK_CIV_EP1","UAZ_Unarmed_TK_CIV_EP1","Volha_1_TK_CIV_EP1","Volha_2_TK_CIV_EP1","VolhaLimo_TK_CIV_EP1"];
            militaryvehs = ["HMMWV","HMMWV_M2","HMMWV_Armored","HMMWV_MK19","HMMWV_TOW","HMMWV_Avenger","MTVR","HMMWV_Ambulance","UAZ_CDF","UAZ_AGS30_CDF","UAZ_MG_CDF","Ural_CDF","UralOpen_CDF","Ural_ZU23_CDF","UAZ_RU","UAZ_AGS30_RU","Kamaz","KamazOpen","Offroad_DSHKM_INS","Pickup_PK_INS","UAZ_INS","UAZ_AGS30_INS","UAZ_MG_INS","UAZ_SPG9_INS","Ural_INS","UralOpen_INS","Ural_ZU23_INS","Offroad_DSHKM_Gue","Offroad_SPG9_Gue","Pickup_PK_GUE","V3S_Gue","Ural_ZU23_Gue","HMMWV_DES_EP1","HMMWV_MK19_DES_EP1","HMMWV_TOW_DES_EP1","HMMWV_M998_crows_M2_DES_EP1","HMMWV_M998_crows_MK19_DES_EP1","HMMWV_M1151_M2_DES_EP1","HMMWV_M998A2_SOV_DES_EP1","HMMWV_Terminal_EP1","HMMWV_M1035_DES_EP1","HMMWV_Avenger_DES_EP1","HMMWV_Avenger_DES_EP1","M1030_US_DES_EP1","MTVR_DES_EP1","HMMWV_Ambulance_DES_EP1","HMMWV_M1151_M2_CZ_DES_EP1","LandRover_CZ_EP1","LandRover_Special_CZ_EP1","MAZ_543_SCUD_TK_EP1","GRAD_TK_EP1","LandRover_MG_TK_EP1","LandRover_SPG9_TK_EP1","SUV_TK_EP1","UAZ_Unarmed_TK_EP1","UAZ_AGS30_TK_EP1","UAZ_MG_TK_EP1","Ural_ZU23_TK_EP1","V3S_TK_EP1","V3S_Open_TK_EP1","LandRover_MG_TK_INS_EP1","LandRover_SPG9_TK_INS_EP1","SUV_UN_EP1","UAZ_Unarmed_UN_EP1","Ural_UN_EP1","Offroad_DSHKM_TK_GUE_EP1","Offroad_SPG9_TK_GUE_EP1","Pickup_PK_TK_GUE_EP1","Ural_ZU23_TK_GUE_EP1","V3S_TK_GUE_EP1"];
        };
        case 1:
        {
            cars = CVG_Cars;
        };
        case 2:
        {
            cars = CVG_Trucks;
        };
        case 3:
        {
            cars = ["HMMWV","HMMWV_M2","HMMWV_Armored","HMMWV_MK19","HMMWV_TOW","HMMWV_Avenger","MTVR","HMMWV_Ambulance","UAZ_CDF","UAZ_AGS30_CDF","UAZ_MG_CDF","Ural_CDF","UralOpen_CDF","Ural_ZU23_CDF","UAZ_RU","UAZ_AGS30_RU","Kamaz","KamazOpen","Offroad_DSHKM_INS","Pickup_PK_INS","UAZ_INS","UAZ_AGS30_INS","UAZ_MG_INS","UAZ_SPG9_INS","Ural_INS","UralOpen_INS","Ural_ZU23_INS","Offroad_DSHKM_Gue","Offroad_SPG9_Gue","Pickup_PK_GUE","V3S_Gue","Ural_ZU23_Gue","HMMWV_DES_EP1","HMMWV_MK19_DES_EP1","HMMWV_TOW_DES_EP1","HMMWV_M998_crows_M2_DES_EP1","HMMWV_M998_crows_MK19_DES_EP1","HMMWV_M1151_M2_DES_EP1","HMMWV_M998A2_SOV_DES_EP1","HMMWV_Terminal_EP1","HMMWV_M1035_DES_EP1","HMMWV_Avenger_DES_EP1","HMMWV_Avenger_DES_EP1","M1030_US_DES_EP1","MTVR_DES_EP1","HMMWV_Ambulance_DES_EP1","HMMWV_M1151_M2_CZ_DES_EP1","LandRover_CZ_EP1","LandRover_Special_CZ_EP1","MAZ_543_SCUD_TK_EP1","GRAD_TK_EP1","LandRover_MG_TK_EP1","LandRover_SPG9_TK_EP1","SUV_TK_EP1","UAZ_Unarmed_TK_EP1","UAZ_AGS30_TK_EP1","UAZ_MG_TK_EP1","Ural_ZU23_TK_EP1","V3S_TK_EP1","V3S_Open_TK_EP1","LandRover_MG_TK_INS_EP1","LandRover_SPG9_TK_INS_EP1","SUV_UN_EP1","UAZ_Unarmed_UN_EP1","Ural_UN_EP1","Offroad_DSHKM_TK_GUE_EP1","Offroad_SPG9_TK_GUE_EP1","Pickup_PK_TK_GUE_EP1","Ural_ZU23_TK_GUE_EP1","V3S_TK_GUE_EP1"];
        };
        case 4:
        {
            cars =  ["Ikarus","SkodaBlue","SkodaGreen","SkodaRed","Skoda","VWGolf","TT650_Civ","MMT_Civ","hilux1_civil_2_covered","hilux1_civil_1_open","hilux1_civil_3_open","car_hatchback","datsun1_civil_1_open","datsun1_civil_2_covered","V3S_Civ","car_sedan","Tractor","UralCivil","UralCivil2","Lada_base","LadaLM","Lada2","Lada1","UAZ_CDF","Ural_CDF","hilux1_civil_3_open_EP1","Ikarus_TK_CIV_EP1","Lada1_TK_CIV_EP1","Lada2_TK_CIV_EP1","Old_bike_TK_CIV_EP1","Old_moto_TK_Civ_EP1","S1203_TK_CIV_EP1","SUV_TK_CIV_EP1","TT650_TK_CIV_EP1","UAZ_Unarmed_TK_CIV_EP1","Volha_1_TK_CIV_EP1","Volha_2_TK_CIV_EP1","VolhaLimo_TK_CIV_EP1","HMMWV","HMMWV_M2","HMMWV_Armored","HMMWV_MK19","HMMWV_TOW","HMMWV_Avenger","MTVR","HMMWV_Ambulance","UAZ_CDF","UAZ_AGS30_CDF","UAZ_MG_CDF","Ural_CDF","UralOpen_CDF","Ural_ZU23_CDF","UAZ_RU","UAZ_AGS30_RU","Kamaz","KamazOpen","Offroad_DSHKM_INS","Pickup_PK_INS","UAZ_INS","UAZ_AGS30_INS","UAZ_MG_INS","UAZ_SPG9_INS","Ural_INS","UralOpen_INS","Ural_ZU23_INS","Offroad_DSHKM_Gue","Offroad_SPG9_Gue","Pickup_PK_GUE","V3S_Gue","Ural_ZU23_Gue","HMMWV_DES_EP1","HMMWV_MK19_DES_EP1","HMMWV_TOW_DES_EP1","HMMWV_M998_crows_M2_DES_EP1","HMMWV_M998_crows_MK19_DES_EP1","HMMWV_M1151_M2_DES_EP1","HMMWV_M998A2_SOV_DES_EP1","HMMWV_Terminal_EP1","HMMWV_M1035_DES_EP1","HMMWV_Avenger_DES_EP1","HMMWV_Avenger_DES_EP1","M1030_US_DES_EP1","MTVR_DES_EP1","HMMWV_Ambulance_DES_EP1","HMMWV_M1151_M2_CZ_DES_EP1","LandRover_CZ_EP1","LandRover_Special_CZ_EP1","MAZ_543_SCUD_TK_EP1","GRAD_TK_EP1","LandRover_MG_TK_EP1","LandRover_SPG9_TK_EP1","SUV_TK_EP1","UAZ_Unarmed_TK_EP1","UAZ_AGS30_TK_EP1","UAZ_MG_TK_EP1","Ural_ZU23_TK_EP1","V3S_TK_EP1","V3S_Open_TK_EP1","LandRover_MG_TK_INS_EP1","LandRover_SPG9_TK_INS_EP1","SUV_UN_EP1","UAZ_Unarmed_UN_EP1","Ural_UN_EP1","Offroad_DSHKM_TK_GUE_EP1","Offroad_SPG9_TK_GUE_EP1","Pickup_PK_TK_GUE_EP1","Ural_ZU23_TK_GUE_EP1","V3S_TK_GUE_EP1"];
        };
    };


//
//
//
//
//
//

////////////////////////////////
////Section 2 Done
progressLoadingScreen 0.4;
/////////////////////////////////

if (isServer) then {
    
    ////// Setup all the weapons
    ////// Server only
    
        //Creates essential logic for spawning. The _pos doesn't matter, but it must be named zombiespawner

        
    Switch (CVG_weapontype) do {
        case 1: {
            //allweapons
            CVG_weapons = [];
            CVG_weapons = CVG_rifles;
            CVG_weapons = CVG_weapons + CVG_Scoped;
            CVG_weapons = CVG_weapons + CVG_Heavy;
            CVG_weapons = CVG_weapons + CVG_pistols;
            CVG_weapons = CVG_weapons + CVG_Launchers;
            
        };
        case 2: {
            //Farmer Guns
            CVG_weapons = ["huntingrifle","colt1911","m9","m9sd","makarov","makarovsd","aks_gold","AK_74","AK_47_M","FN_FAL","AK_47_S","AKS_74_U","LeeEnfield","m16a2","m1014"];
            
            
        };
        case 3: {
            //Blufor Weapons
            CVG_weapons = ["m24","m40a3","m107","m4spr","glock17_EP1","DMR","smaw","smaw","BAF_AS50_scoped","BAF_AS50_TWS","BAF_L110A1_Aim","BAF_L7A2_GPMG","BAF_L85A2_RIS_ACOG","BAF_L85A2_RIS_CWS","BAF_L85A2_RIS_Holo","m1014","m1014","m1014","m1014","BAF_L85A2_RIS_SUSAT","m8_carbine_pmc","m8_carbineGL","m8_SAW","m8_sharpshooter","m8_compact","BAF_L85A2_UGL_ACOG","AA12_PMC","m8_compact_pmc","BAF_L85A2_UGL_Holo","m8_holo_sd","BAF_L85A2_UGL_SUSAT","BAF_L86A2_ACOG","m8_tws_sd","BAF_LRR_scoped","m8_tws","BAF_LRR_scoped_W","colt1911","m9","m9sd","m16a2","m16a2gl","m16a4","m16a4_gl","m16a4_acg","m16a4_acg_gl","m4a1","m4a1_aim","m4A1_AIM_camo","SCAR_H_CQC_CCO_SD","SCAR_H_STD_EGLM_Spect","SCAR_H_STD_TWS_SD","Sa58P_EP1","Sa58V_EP1","Sa58V_RCO_EP1","Sa58V_CCO_EP1","SCAR_H_LNG_Sniper","SCAR_H_LNG_Sniper_SD","SCAR_L_STD_HOLO","SCAR_L_STD_Mk4CQT","SCAR_H_CQC_CCO","M4A1_AIM_SD_camo","SCAR_L_STD_EGLM_TWS","M4A1_HWS_GL","m4a1_hws_gl_camo","m4a1_hws_gl_sd_camo","m4a1_rco_gl","m8_carbine","m8_carbinegl","m8_compact","m8_sharpshooter","m240_scoped_EP1","M249_EP1","M249_m145_EP1","M110_TWS_EP1","m107_TWS_EP1","M249_TWS_EP1","M60A4_EP1","Mk_48_DES_EP1","g36a","g36k","g36c","g36_c_sd_eotech","mp5a5","mp5sd","m1014","MG36_camo","m249","m240","mk_48","mg36","m1014","m8_saw","M14_EP1","M4A3_RCO_GL_EP1","M4A3_CCO_EP1","SCAR_L_CQC_CCO_SD","SCAR_L_CQC","SCAR_L_CQC_Holo","SCAR_L_CQC_EGLM_Holo","SCAR_L_STD_EGLM_RCO"];
            
            
        };
        case 4: {
            //OpFor Weapons
            CVG_weapons = ["vss_vintorez","svd","svd_camo","SVD_NSPU_EP1","ksvk","rpg7v","pk","pecheneg","makarov","SVD_des_EP1","UZI_SD_EP1","UZI_EP1","revolver_gold_EP1","revolver_EP1","makarovsd","saiga12k","bizon","aks_gold","AK_74","AK_47_M","FN_FAL","RPK_74","PK","AK_74_GL_kobra","AK_47_S","AKS_74_pso","AKS_74_GOSHAWK","AKS_74_U","LeeEnfield"];
            
            
        };
        case 5: 
        {
            //pistols
            CVG_weapons = CVG_pistols;
        };
        
        case 6:
        {
            
        };
    };
    
    //prepares some functions to run later
    randomWeapons =		 compile preprocessFileLineNumbers "craigs_scripts\randomWeapons.sqf";
    vehicleInfo =		 compile preProcessFileLineNumbers "craigs_scripts\dvs\vehicleinfo.sqf";
    objSpawn = 		compile preProcessFileLineNumbers "craigs_scripts\dvs\ObjectSpawn.sqf";
    populateIsland =		compile preProcessFileLineNumbers "craigs_scripts\populateIsland.sqf";
    [] call populateIsland;

        
        
        /*
        
        The Zombie Trigger creating begins. The first section spawns zombie triggers in a grid
        
        */
        
        if (CVG_Zombietowns == 2) then {
            _xaxis = 0;
            _yaxis = 0;
            _working = true;
            while {_working} do {
                _yaxis = _yaxis + 1000;
                if (_yaxis == 20000) then {
                    _xaxis = _xaxis + 1000;
                    _yaxis = -5000;
                };
                
                _pos = [_xaxis,_yaxis];
                _trig = createTrigger ["EmptyDetector", _pos];
                trigArray = trigArray + [_trig];
                _trig setTriggerArea [500,500,0,true];
                _trig setTriggerActivation ["NONE", "PRESENT", FALSE];
                if (CVG_Horde == 2) then {
                    _trig setTriggerStatements ["true", "0=[thisTrigger,-1] execVM 'zombie_scripts\cly_z_generatorhorde.sqf';", ""];
                };
                if (CVG_Horde == 1) then {
                    _trig setTriggerStatements ["true", "0=[thisTrigger,-1] execVM 'zombie_scripts\cly_z_generator.sqf';", ""];
                };
                if (cvg_debug == 2) then {
                    _marker=createMarker [format ["mar%1",random 100000],getpos _trig];
                    _marker setMarkerType "Dot";
                    _marker setMarkerColor "ColorRed";
                    _marker setMarkerSize [1,1];
                };
                _allaxiss = _xaxis + _yaxis;
                if (_allaxiss == 40000) then {_working = false;};
            };
        };
        
        
        

        
        //Spawns some more zombies, if zombietowns equals three
        if (CVG_Zombietowns == 3) then {
            
            _xaxis = 0;
            _yaxis = 0;
            _working = true;
            while {_working} do {
                _yaxis = _yaxis + 1000;
                if (_yaxis == 20000) then {
                    _xaxis = _xaxis + 1000;
                    _yaxis = -5000;
                };
                
                _pos = [_xaxis,_yaxis];
                _trig = createTrigger ["EmptyDetector", _pos];
                trigArray = trigArray + [_trig];
                _trig setTriggerArea [500,500,0,true];
                _trig setTriggerActivation ["NONE", "PRESENT", FALSE];
                if (CVG_Horde == 2) then {
                    _trig setTriggerStatements ["true", "0=[thisTrigger,-1] execVM 'zombie_scripts\cly_z_generatorhorde.sqf';", ""];
                };
                if (CVG_Horde == 1) then {
                    _trig setTriggerStatements ["true", "0=[thisTrigger,-1] execVM 'zombie_scripts\cly_z_generator.sqf';", ""];
                };
                if (cvg_debug == 2) then {
                    _marker=createMarker [format ["mar%1",random 100000],getpos _trig];
                    _marker setMarkerType "Dot";
                    _marker setMarkerColor "ColorRed";
                    _marker setMarkerSize [1,1];
                };
                _allaxiss = _xaxis + _yaxis;
                if (_allaxiss == 40000) then {_working = false;};
            };
        };
        
        
        //Checks for fuel
        if (CVG_Fuel == 1) then {
            [] execVM "Craigs_scripts\removeGasStations.sqf";
        };
        
        //Sets time
        if (CVG_debug == 2) then {
            player sidechat "Setting Time";
        };
        skipTime CVG_timeToSkipTo;
        if (CVG_debug == 2) then {
            player sidechat "Time Set";
        };
        
        
        ///Ends the server part
    };
    //This part is server and client
    
    
    progressLoadingScreen 0.6;
    ///////////////////////////
    //////////////////////////
    //Clients!!\\
    //////////////////////////
    ///////////////////////
    if (!isDedicated) then {
		if (player != player) then
		{
			waitUntil {player == player};
			waitUntil {time > 10};
		};
		if (time > 1) then {
			waitUntil {local Player};
		};
		if (CVG_debug == 2) then {
			player sidechat "loading clients";
		};
    };
    
    //This section spawns some more modules, dedicated doesn't run the first three, because it has no need to
    _side = createCenter sideLogic;
    _group = createGroup _side;
    if (!(isDedicated)) then {
        _logic = _group createUnit ["WeatherParticlesManager", [0,0,0], [], 0, "NONE"];
        _logic = _group createUnit ["WeatherPostprocessManager", [0,0,0], [], 0, "NONE"];
        _logic = _group createUnit ["BIS_clouds_Logic", [0,0,0], [], 0, "NONE"];
		_logic = _group createUnit ["PreloadManager", [0,0,0], [], 0, "NONE"];
    };

    //Spawns animals
    if (CVG_Aminals == 1) then {_pos = [110,110];   _group = createGroup sideLogic;  aminals = _group createUnit ["BIS_animals_Logic", _pos, [], 0, "NONE"]};
    
    //Starts fasttime script
    if (CVG_fastTime == 1) then {
        [4] execFSM "craigs_scripts\core_time.fsm";
    };
    
    
    ///This block  of code spawns the building destruction. Executes on all clients
    if (CVG_CityDestruction == 2) then {
        if (CVG_debug == 2) then {
            player sidechat "loading building destruction";
        };
        _newPos = getArray(configFile >> "CfgWorlds" >> worldName >> "centerPosition");
        [_newPos,10000,0,[]] call bis_fnc_destroyCity;
        if (CVG_debug == 2) then {
            player sidechat "Bulding destruction done";
        };
    };
    
    
    //Should the player have weapons?
    if (CVG_playerWeapons == 1) then {
        if (CVG_debug == 2) then {player sidechat "picking starting weapon"};
        removeAllWeapons player;
        [player] execVM "craigs_scripts\randomUnitWeapons.sqf";
    };
    
    
    
    
    if (CVG_playerWeapons == 2) then 
    {
        removeAllWeapons player;
    };
    
    
    //Should the player have a map?? 
    if (CVG_playerItems == 2) then {player removeWeapon "ItemMap"};
    
    
    
    
    
    //Does some more things invovling checking if the player is ready to be worked on
 
    
    
    
    
    //Moves loading screen some more, this probably won't show up, but it's more for aesthetics in the code
    progressLoadingScreen 0.8;
    
    
    
    /* 
    All of this chooses where the player should spawn at start. The first set, playerstart 50, puts the player in a random town. 
    The second, 100, puts the player in a random position outside of a town
    The third, puts the player at the center position with all other players
    */
    if (CVG_debug == 2) then {player sidechat "Picking location"};
    _towns= townlist;
    
    if (CVG_Playerstart == 50) then {
        if ((count _towns) != 0) then {
            _townnumber = floor (random (count _towns));
            _town = _towns select _townnumber;
            _townpos = (position _town);
            _group = createGroup sideLogic;
            _logic = _group createUnit ["Logic",_townpos, [], 100, "NONE"];   
            _newPos = position _logic;
			if ((_newPos select 0) != 0) then {
            player setpos _newPos;
			} else {
			player setpos _townPos
			};
            if (CVG_debug == 2) then {
                player sidechat format ["location chosen, %1",_town]
            };
        }
        else
        {
            if ((count buildings) != 0) then {
                _building = (round(random(count buildings)));
                _newpos = position (buildings select _building);
                _newpos = [_newPos,0,50,1,0,20,0] call BIS_fnc_findSafePos;
                player setpos _newpos;
            }
            else
            {
                _things = nearestObjects [player, [], 200000];
                if ((count _things) != 0) then {
                    _thing = (round(random(count _things)));
                    _newpos = position (_things select _thing);
                    player setpos _newpos;
                }
                else
                {
                    _newpos = [(random 1000),(random 1000)];
                    player setpos _newpos;
                };
            };
        };
		deleteVehicle _logic;	
	
	    if (CVG_debug == 2) then {
			player sidechat format ["location chosen, %1",_newPos];
		};
    };
    
    
    if (CVG_playerstart == 100) then {
        _townnumber = floor (random (count _towns));
        _town = _towns select _townnumber;
        _townpos = getpos _town;
        _checkVar = 0;
        _armPos = getArray(configFile >> "CfgWorlds" >> worldName >> "Armory" >> "positionStart");
        _centPos = getArray(configFile >> "CfgWorlds" >> worldName >> "centerPosition");
	
        while {_checkVar < 1} do{
            _dist = 1500;
            _dir = random 360;
            _pos = [(_townpos select 0) + (sin _dir) * _dist, (_townpos select 1) + (cos _dir) * _dist, 0];
            _pos = [_pos,0,40,4,0,20,0] call BIS_fnc_findSafePos;
            _check1 = [_pos, _armPos] call arrayCompare;
            _check2 = [_pos,_centPos] call arrayCompare;
            if ((!_check1) && (!_check2) && ((_pos distance (getpos _town)) > 1000)) then { _checkVar = 1};
        };
        player setpos _pos;
        if (CVG_debug == 2) then {player sidechat format ["location chosen, %1",_townpos]};
    };
    

    
    
    if (CVG_playerstart == 150) then {
        if (isServer) then {
				if (count _towns != 0) then {
				_townnumber = floor (random (count _towns));
				_town = _towns select _townnumber;
				newPos = (position _town);
				if (CVG_debug == 2) then {
					player sidechat format ["location chosen, %1",_town]
				};
			}
			else
			{
				if ((count buildings) != 0) then {
					_building = (round(random(count buildings)));
					_newpos = position (buildings select _building);
					newPos = [_newPos,0,50,1,0,20,0] call BIS_fnc_findSafePos;
				}
				else
				{
					_things = nearestObjects [player, [], 200000];
					if ((count _things) != 0) then {
						_thing = (round(random(count _things)));
						newPos = position (_things select _thing);
					}
					else
					{
						newPos = [(random 1000),(random 1000),0];
					};
				};
			};
		publicVariable "newPos";
        };
		waitUntil {!(isNil "newPos")};
		player setpos newPos;
        
        
        if (CVG_debug == 2) then {
            player sidechat format ["location chosen, %1",_newPos];
        };
    };
    
    
    
    //Sets up respawn
    _playerRespawn = player addMPEventHandler ["MPRespawn", {Null = _this execVM "craigs_scripts\playerRestart.sqf";}]; 
    
	//EH
	_EHkilled = _zombie addEventHandler ["killed", {_this execVM "craigs_scripts\onDeath.sqf"}];

    
    
    //Loading screen ends
    endLoadingScreen;
    
    screendone = 1;
    
    /////////////////////After mission starts
    sleep .1;
    //After mission started
    ////////////////////////NO TOUCHY
    //Runs refresher and horde spawner, added in .95
    //[] execVM "craigs_scripts\refresher.sqf";
    //[] execVM "craigs_scripts\hordeSpawner.sqf"; //Spawns big hordes that hunt you down.
    
    
    
    //Weather
    switch (CVG_Weather) do {
        case 1:
        {0 setFog 0;
        0 setOvercast 0;};
        case 2: 
        {
            0 setFog 0;
            0 setOvercast 1;
        };
        case 3: 
        {
            0 setFog 0.5;
            0 setOvercast 0.5;
        };
        case 4: 
        {
            0 setFog 1;
            0 setOvercast 1;
        };
        case 5:
        {
        	if (isServer) then 
        	{
        		fogNumber = (random 1);
        		overcastNumber = (random 1);
        		publicVariable "fogNumber";
        		publicVariable "overcastNumber";
        	};
        	waitUntil {(!(isNil "fogNumber")) && (!(isNil "overcastNumber"))};
        	0 setFog fogNumber;
        	0 setOvercast overcastNumber;
        };
        
    };
    
    //View distances
    setViewDistance CVG_Viewdist;
    //Fog
    if (CVG_fog == 1) then {null=[player,100,11,30,3,7,-0.3,0.1,0.5,1,1,1,13,12,15,false,2,2.1,0.1,1,1,0,0,24] execFSM "Fog.fsm"};
    
    /*
    if (CVG_playerWeapons == 2) then 
    {
        if (count (weapons player) != 0) then 
        {
        	while {count (weapons player) != 0} do
        	{
        		removeAllWeapons player;
        	};
        	diag_log "Player now has no weapons";
        };
    };
    
    */
    
    if (isDedicated) exitWith {};
    //Should the player have a map?? 
    if (CVG_playerItems == 2) then 
    {
    	if (player hasWeapon "ItemMap") then 
    	{
    		while {player hasWeapon "itemMap"} do 
    		{
    			player removeWeapon "itemMap";
    		};  
    	diag_log "Player now has no map";
    	};
    };


    sleep 5;
    
if (CVG_NVG == 1) then {
	if (!(player hasWeapon "NVgoggles")) then {
		player addWeapon "NVgoggles";
		if (!(player hasWeapon "NVgoggles")) then {
			diag_log "Adding NV Goggles Failed, Trying again";
			while {!(player hasWeapon "NVgoggles")} do {
				player addWeapon "NVgoggles";
				diag_log "Trying to add NV Goggles again";
			};
		};
	};
	diag_log "NV Goggles added successfully";
};
    //WOOT Done. Let the zombie slaying begin.
    
    [] execVM "craigs_scripts\sirenpoles.sqf";
	sleep 15;
    if (CVG_ZombieTowns == 4) then {
		[] execVM "craigs_scripts\zombieGenerator.sqf";
    };